package org.kunteng.yunac.lttask.controller.view;


import org.kunteng.yunac.lttask.AbstractTask;
import org.kunteng.yunac.lttask.LTTaskManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by hedingwei on 6/5/15.
 */
@Controller
@RequestMapping(value ="${adminPath}/f/lttask")
public class Init {

    private ModelAndView init = new ModelAndView("f/lttask/init");

    @Autowired
    private LTTaskManager longTimeTaskRunner;

    @RequestMapping(value = "init.view.do")
    public ModelAndView action(HttpServletRequest request,@RequestParam("taskId")String taskId) {

        System.out.println("taskId:    "+taskId);
        ModelAndView status = new ModelAndView("redirect:status.view.do?taskId="+taskId);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(longTimeTaskRunner.getRunningTask(taskId)==null){
            if(longTimeTaskRunner.isRegisteredTask(taskId)){
                AbstractTask abstractTask = longTimeTaskRunner.getLastTask(taskId);
                if(abstractTask==null){
                    init.addObject("hasHistoryTask",false);
                }else{
                    init.addObject("hasHistoryTask",true);
                    init.addObject("task_model",abstractTask.getModel());
                }
                init.addObject("isRunning", false);
                init.addObject("isUnRegisteredTask",false);
                return init;
            }else{
                AbstractTask abstractTask = longTimeTaskRunner.getLastTask(taskId);
                if(abstractTask==null){
                    init.addObject("hasHistoryTask",false);
                }else{
                    init.addObject("hasHistoryTask",true);
                    init.addObject("task_model",abstractTask.getModel());
                }
                init.addObject("isRunning", false);
                init.addObject("isUnRegisteredTask",true);
                return init;
            }
        }else{
            return status;
        }
    }


    @RequestMapping("${adminPath}/f/sd/detectlog/synch/init1.view.do")
    public ModelAndView action1(HttpServletRequest request,@RequestParam("taskId")String taskId) {
        System.out.println("第三次进来");
        ModelAndView init1 = new ModelAndView("${adminPath}/f/lttask/init1");
        System.out.println("taskId:    "+taskId);
        ModelAndView status = new ModelAndView("redirect:${adminPath}/f/sd/detectlog/synch/status1.view.do?taskId="+taskId);

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if(longTimeTaskRunner.getRunningTask(taskId)==null){
            System.out.println(longTimeTaskRunner.getRunningTask(taskId)==null);

            if(longTimeTaskRunner.isRegisteredTask(taskId)){
                AbstractTask abstractTask = longTimeTaskRunner.getLastTask(taskId);
                if(abstractTask==null){
                    init1.addObject("hasHistoryTask",false);
                }else{
                    init1.addObject("hasHistoryTask",true);
                    init1.addObject("task_model",abstractTask.getModel());
                }
                init1.addObject("isRunning", false);
                init1.addObject("isUnRegisteredTask",false);
                if(taskId.equals("sd_dclog_export_today")){
                    init1.addObject("url","${adminPath}/f/sd/detectlog/download/list.do");
                }
                if(taskId.equals("sd_dclog_import_today")){
                    init1.addObject("url","${adminPath}/f/sd/detectlog/list.do");
                }
                return init1;
            }else{
                AbstractTask abstractTask = longTimeTaskRunner.getLastTask(taskId);
                if(abstractTask==null){
                    init1.addObject("hasHistoryTask",false);
                }else{
                    init1.addObject("hasHistoryTask",true);
                    init1.addObject("task_model",abstractTask.getModel());
                }
                if(taskId.equals("sd_dclog_export_today")){
                    init1.addObject("url","${adminPath}/f/sd/detectlog/download/list.do");
                }
                if(taskId.equals("sd_dclog_import_today")){
                    init1.addObject("url","${adminPath}/f/sd/detectlog/list.do");
                }
                init1.addObject("isRunning", false);
                init1.addObject("isUnRegisteredTask",true);
                return init1;
            }
        }else{
            return status;
        }
    }
}
